import json
from flask import Blueprint, jsonify,request

from handle.logger import logger
from handle.warehouses import *

warehouses_bp = Blueprint('warehouses', __name__)

@warehouses_bp.route('/', methods=['GET'])
def get_warehouses():
    logger.info(get_warehouses_list().decode('utf-8'))
    data=json.loads(get_warehouses_list().decode('utf-8').replace("null",'"null"'))
    return json.dumps({"status": 200,"message": "Get warehouse list","data": data},sort_keys=False)

@warehouses_bp.route('/<SKU>', methods=['PUT'])
def stock_in(SKU):
    Stock_num=request.get_json()
    Status,data=in_warehouses_SKU(SKU,Stock_num["Stock"])
    if Status==True and data!=None:
        return json.dumps({"status": 200,"message": f"stock_in SKU: {SKU}","Stock": data},sort_keys=False)

@warehouses_bp.route('/<SKU>', methods=['DELETE'])
def stock_out(SKU):
    Stock_num=request.get_json()
    Status,data=out_warehouses_SKU(SKU,Stock_num["Stock"])
    if Status==True and data!=None:
        return json.dumps({"status": 200,"message": f"stock_in SKU: {SKU}","Stock": data},sort_keys=False)
